package com.example.zhou.zhihunews;

import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.load.model.GlideUrl;
import com.example.zhou.base.BaseActivity;
import com.example.zhou.callback.NewsInfoCallback;
import com.example.zhou.connect.NewsInfoConnect;
import com.example.zhou.model.NewsInfo;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class NewsInfoActivity extends BaseActivity implements NewsInfoCallback {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_title)
    ImageView imageView;
    @BindView(R.id.webView)
    WebView webView;
    private NewsInfo newsInfo;
    @BindView(R.id.nestedScrollview)
    NestedScrollView scrollView;


    private NewsInfoConnect newsInfoConnect = new NewsInfoConnect();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_info);
        ButterKnife.bind(this);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               // webView.setVisibility(View.GONE);
                supportPostponeEnterTransition();
                supportFinishAfterTransition();
            }
        });

        String id = getIntent().getExtras().getString("ID");
        newsInfoConnect.getNewsInfo(id, this);

        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollX == 0 && scrollY == 0) {

                }

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }

    @Override
    public void onNewInfoSuccess(String success) {
        Log.i("success", success);
        newsInfo = JSON.parseObject(success, NewsInfo.class);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                requestBuilder.load(new GlideUrl(newsInfo.getImage())).into(imageView);
               /* webView.loadData(newsInfo.getBody(), "text/html", "UTF-8");
                webView.loadData(newsInfo.getCss().get(0), "text/css", "UTF-8");*/


                StringBuilder sb = new StringBuilder();
                sb.append("<div class=\"img-wrap\">")
                        .append("<div class=\"img-mask\"></div>");
                String mNewsContent = "<link rel=\"stylesheet\" type=\"text/css\" href=\"news_content_style.css\"/>"
                        + "<link rel=\"stylesheet\" type=\"text/css\" href=\"news_header_style.css\"/>"
                        + newsInfo.getBody().replace("<div class=\"img-place-holder\">", sb.toString());
                webView.loadDataWithBaseURL("file:///android_asset/", mNewsContent, "text/html", "UTF-8", null);
               /* OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(newsInfo.getCss().get(0)).build();
                try {
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String css = response.body().string();

                    } else {
                        Log.e("something went wrong", "something");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
            }
        });
    }

    @Override
    public void onNewInfoFail(String fail) {

    }
}
